package com.yxy.patient.widiget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yxy.patient.R;
import com.yxy.patient.databinding.TitleViewBinding;

public class TitleView extends FrameLayout {
    private TitleViewBinding titleViewBinding;
    private String titleViewText = "";
    private String rightText = "";
    private boolean hasBackButton = true;

    public TitleView(@NonNull Context context) {
        super(context);
    }

    public TitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initWithAttrs(attrs);
    }

    public TitleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initWithAttrs(attrs);
    }

    private void initWithAttrs(AttributeSet attributeSet) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.TitleView);
        titleViewText = typedArray.getString(R.styleable.TitleView_titleText);
        rightText = typedArray.getString(R.styleable.TitleView_rightText);
        hasBackButton = typedArray.getBoolean(R.styleable.TitleView_hasBackButton, true);
        typedArray.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View root = LayoutInflater.from(getContext()).inflate(R.layout.title_view, this, false);
        titleViewBinding = TitleViewBinding.bind(root);
        titleViewBinding.title.setText(titleViewText);
        titleViewBinding.title.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        titleViewBinding.title.setSingleLine(true);
        titleViewBinding.title.setSelected(true);
        titleViewBinding.title.setFocusable(true);
        titleViewBinding.title.setFocusableInTouchMode(true);

        titleViewBinding.back.setOnClickListener(v -> {
            if (getContext() instanceof Activity) {
                ((Activity) getContext()).finish();
            }
        });
        if (hasBackButton) {
            titleViewBinding.back.setVisibility(View.VISIBLE);
        } else {
            titleViewBinding.back.setVisibility(View.INVISIBLE);
        }
        if (rightText == null || rightText.isEmpty()) {
            titleViewBinding.rightButton.setVisibility(View.INVISIBLE);
        } else {
            titleViewBinding.rightButton.setVisibility(View.VISIBLE);
            titleViewBinding.rightButton.setText(rightText);
        }
        addView(root);
    }

    public void setTitle(String title) {
        titleViewBinding.title.setText(title);
        titleViewBinding.title.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        titleViewBinding.title.setSingleLine(true);
        titleViewBinding.title.setSelected(true);
        titleViewBinding.title.setFocusable(true);
        titleViewBinding.title.setFocusableInTouchMode(true);
    }

    public void setOnRightTextClicked(OnClickListener onClickListener) {
        titleViewBinding.rightButton.setOnClickListener(onClickListener);
    }

    public void setOnBackTextClicked(OnClickListener onClickListener) {
        titleViewBinding.back.setOnClickListener(onClickListener);
    }
}
